using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace KCD.SharePoint
{
  
public class EditorControl : UserControl, IFieldEditor
{
#region Properties
    protected DropDownList ddlLists;
    protected DropDownList ddlColumns;
    protected DropDownList ddlColumns1;
    protected DropDownList ddlColumns2;
    protected DropDownList ddlColumns3;
    protected DropDownList ddlColumns4;
    protected Label Label1;
    protected Label Label2;
    protected Label Label3;
    protected LinkButton AddColumn;
    protected LinkButton CloseColumn;
    protected TextBox txtSiteURL;
    protected Button btnLoadLists;
    private string valueSiteURL = default(string);
    private string valueList = default(string);
    private string valueColumn = default(string);
#endregion
    
  
    string[] DetermineVariable(string Text)
    {
        return Text.Split('#');

    }
    public void InitializeWithField(SPField field)
    {
        CrossSiteLookup CrossSiteLookup = field as CrossSiteLookup;
        if (CrossSiteLookup != null)
        {
            EnsureChildControls();
            string sDelimitedPropertyValues = (string)CrossSiteLookup.GetCustomProperty("DelimitedPropertyValues");
            
            string[] List = DetermineVariable(sDelimitedPropertyValues);
            
            string sSiteURL = List[0];
            string sListName = List[1];
            string sColumnName = List[2];
            string sconnected = List[3];
            string sconnected2 = string.Empty;
           string sconnected3=string.Empty;
            string sconnected4=string.Empty;
            if (List.Length == 5)
            {
                sconnected2 = List[4];

            }

            if (List.Length == 6)
            {
                sconnected2 = List[4];
                sconnected3 = List[5];
            }

            if (List.Length == 7)
            {
                sconnected2 = List[4];
                  sconnected3 = List[5];
                  sconnected4 = List[6];
            }
            

            if (string.IsNullOrEmpty(txtSiteURL.Text))
            {
                
              
                if (List.Length == 4)
                {
                    txtSiteURL.Text = sSiteURL;
                    this.btnLoadLists_Click(null, null);
                    ddlLists.Items.FindByText(sListName).Selected = true;
                    this.ddlLists_SelectedIndexChanged(null, null);
                    ddlColumns.Items.FindByText(sColumnName).Selected = true;
                    ddlColumns_SelectedIndexChanged(ddlColumns, null);
                    ddlColumns1.Items.FindByText(sconnected).Selected = true;
                    ddlColumns1.Visible = true;
                   
                }
                if (List.Length == 5)
                {

                    txtSiteURL.Text = sSiteURL;
                    this.btnLoadLists_Click(null, null);
                    ddlLists.Items.FindByText(sListName).Selected = true;
                    this.ddlLists_SelectedIndexChanged(null, null);
                    ddlColumns.Items.FindByText(sColumnName).Selected = true;
                    ddlColumns_SelectedIndexChanged(ddlColumns, null);
                    ddlColumns1.Items.FindByText(sconnected).Selected = true;
                    ddlColumns1_SelectedIndexChanged(ddlColumns1, null);
                    ddlColumns2.Items.FindByText(sconnected2).Selected = true;
                    ddlColumns1.Visible = true;
                    ddlColumns2.Visible = true;
                }
                if (List.Length == 6)
                {
                    txtSiteURL.Text = sSiteURL;
                    this.btnLoadLists_Click(null, null);
                    ddlLists.Items.FindByText(sListName).Selected = true;
                    this.ddlLists_SelectedIndexChanged(null, null);
                    ddlColumns.Items.FindByText(sColumnName).Selected = true;
                    ddlColumns_SelectedIndexChanged(ddlColumns, null);
                    ddlColumns1.Items.FindByText(sconnected).Selected = true;
                    ddlColumns1_SelectedIndexChanged(ddlColumns1, null);
                    ddlColumns2.Items.FindByText(sconnected2).Selected = true;
                    ddlColumns2_SelectedIndexChanged(ddlColumns2, null);
                    ddlColumns3.Items.FindByText(sconnected3).Selected = true;
                    ddlColumns1.Visible = true;
                    ddlColumns2.Visible = true;
                    ddlColumns3.Visible = true;

                }
                if (List.Length == 7)
                {
                    txtSiteURL.Text = sSiteURL;
                    this.btnLoadLists_Click(null, null);
                    ddlLists.Items.FindByText(sListName).Selected = true;
                    this.ddlLists_SelectedIndexChanged(null, null);
                    ddlColumns.Items.FindByText(sColumnName).Selected = true;
                    ddlColumns_SelectedIndexChanged(ddlColumns, null);
                    ddlColumns1.Items.FindByText(sconnected).Selected = true;
                    ddlColumns1_SelectedIndexChanged(ddlColumns1, null);
                    ddlColumns2.Items.FindByText(sconnected2).Selected = true;
                    ddlColumns2_SelectedIndexChanged(ddlColumns2, null);
                    ddlColumns3.Items.FindByText(sconnected3).Selected = true;
                    ddlColumns3_SelectedIndexChanged(ddlColumns3, null);
                    ddlColumns4.Items.FindByText(sconnected4).Selected = true;
                    ddlColumns1.Visible = true;
                    ddlColumns2.Visible = true;
                    ddlColumns3.Visible = true;
                    ddlColumns4.Visible = true;
                }



              








               
            }        
        }
    }
    private string  SetValueWithSharp(DropDownList ddl,string ddlvalue,string value)
    {
        if (ddl.Visible == true)
        {
            value = value + "#" + ddlvalue;
        }
        return value;
    }
 
    public void OnSaveChange(SPField field, bool isNew)
    {
        string sListName = this.ddlLists.SelectedValue;
        string sColumnName = this.ddlColumns.SelectedValue;
        string sConnectedCol = this.ddlColumns1.SelectedValue;
        
        string sSiteURL = this.txtSiteURL.Text;

        CrossSiteLookup Lookup = field as CrossSiteLookup;
        string value = sSiteURL + '#' + sListName + '#' + sColumnName+'#'+sConnectedCol;

        value = SetValueWithSharp(ddlColumns2, ddlColumns2.SelectedValue, value);
        value = SetValueWithSharp(ddlColumns3, ddlColumns3.SelectedValue, value);
        value = SetValueWithSharp(ddlColumns4, ddlColumns4.SelectedValue, value);





        if (isNew)

            Lookup.UpdateMyCustomProperty(value);

        else

            Lookup.DelimitedPropertyValues = value;
    }

  
    public bool DisplayAsNewSection
    {
        get
        {
            return true;
        }
    }

  
    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        this.CloseColumn.Click += new EventHandler(CloseColumn_Click);
        this.AddColumn.Click += new EventHandler(AddColumn_Click);
        this.btnLoadLists.Click += new EventHandler(btnLoadLists_Click);
        this.ddlLists.AutoPostBack = true;

        this.ddlLists.EnableViewState = true;
        this.ddlLists.SelectedIndexChanged += new EventHandler(ddlLists_SelectedIndexChanged);
        this.ddlColumns.AutoPostBack = true;
        this.ddlColumns1.AutoPostBack = true;
        this.ddlColumns2.AutoPostBack = true;
        ddlColumns3.AutoPostBack = true;
        this.ddlColumns.EnableViewState = true;
        this.ddlColumns.SelectedIndexChanged += new EventHandler(ddlColumns_SelectedIndexChanged);
        this.ddlColumns1.SelectedIndexChanged += new EventHandler(ddlColumns1_SelectedIndexChanged);
        this.ddlColumns2.SelectedIndexChanged += new EventHandler(ddlColumns2_SelectedIndexChanged);
        this.ddlColumns3.SelectedIndexChanged += new EventHandler(ddlColumns3_SelectedIndexChanged);
        if (!this.IsPostBack)
        {
            ddlLists.SelectedValue = this.valueSiteURL;
            txtSiteURL.Text = this.valueList;
            ddlColumns.SelectedValue = this.valueColumn;
        }
    }

    void ddlColumns3_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillDropDownList((DropDownList)sender, ddlColumns4);
    }

    void ddlColumns2_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillDropDownList((DropDownList)sender, ddlColumns3);
    }

    void ddlColumns1_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillDropDownList((DropDownList)sender, ddlColumns2);
    }

    void CloseColumn_Click(object sender, EventArgs e)
    {
        List<DropDownList> Drps = new List<DropDownList>();
        Drps.Add(ddlColumns4);
        Drps.Add(ddlColumns3);
        Drps.Add(ddlColumns2);
        List<Label> DrpsL = new List<Label>();
        DrpsL.Add(Label3);
        DrpsL.Add(Label2);
        DrpsL.Add(Label1);
        for (int i = 0; i < 3; i++)
        {
            if (Drps[i].Visible == true)
            {
                DrpsL[i].Visible = false;
                Drps[i].Visible = false;
                break;
            }
        }
    }

    void AddColumn_Click(object sender, EventArgs e)
    {
        List<DropDownList> Drps = new List<DropDownList>();
        Drps.Add(ddlColumns2);
        Drps.Add(ddlColumns3);
        Drps.Add(ddlColumns4);

        List<Label> DrpsL = new List<Label>();
        DrpsL.Add(Label1);
        DrpsL.Add(Label2);
        DrpsL.Add(Label3);
        for (int i = 0; i < 3; i++)
        {
            if (Drps[i].Visible == false)
            {
                DrpsL[i].Visible = true;
                Drps[i].Visible = true;
                break;
            }
        }
    }
    private void FillDropDownList(DropDownList PrevDr,  DropDownList dr)
    {
        SPSite site = null;
        SPWeb web = null;
        try
        {
            if (this.ddlLists.SelectedItem != null)
            {
                EnsureChildControls();
                dr.Items.Clear();
                 
                foreach (ListItem oneField in PrevDr.Items)
                {
                    if (!PrevDr.SelectedItem.Value.Equals(oneField.Value))
                    {



                        dr.Items.Add(oneField.Value);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            this.txtSiteURL.Text = ex.Message;
        }
        finally
        {
           
        }

    }


    void ddlColumns_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillDropDownList((DropDownList)sender, ddlColumns1);
    }


    protected void btnLoadLists_Click(object sender, EventArgs e)
    {
        SPSite site = null;
        SPWeb web = null;
        try
        {
            EnsureChildControls();
            ddlLists.Items.Clear();
            site = new SPSite(this.txtSiteURL.Text);
            if (site != null)
            {
                web = site.OpenWeb();
                foreach (SPList list in web.Lists)
                {
                    ddlLists.Items.Add(list.Title);
                }
            }
        }
        catch (Exception ex)
        {
            this.txtSiteURL.Text = ex.Message;
        }
        finally
        {
            site.Dispose();
            web.Dispose();
        }
    }

 
    protected void ddlLists_SelectedIndexChanged(object sender, EventArgs e)
    {
        SPSite site = null;
        SPWeb web = null;
        try
        {
            if (this.ddlLists.SelectedItem != null)
            {
                EnsureChildControls();
                ddlColumns.Items.Clear();
                site = new SPSite(txtSiteURL.Text);
                web = site.OpenWeb();
                SPList list = web.Lists[this.ddlLists.SelectedItem.Text];
                foreach (SPField oneField in list.Fields)
                {
                    ddlColumns.Items.Add(oneField.Title);
                }
            }
        }
        catch (Exception ex)
        {
            this.txtSiteURL.Text = ex.Message;
        }
        finally
        {
            site.Dispose();
            web.Dispose();
        }
    }


   }
}


